package com.itheima.tlias84.config;

import com.aliyun.oss.OSS;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 阿里云Oss客户端
 */
@Component
public class OssClient {

    @Resource
    OSS ossBean;

    @Resource
    AliyunOssConfig config;
    /**
     * 阿里云上传图片方法
     * @param fileName 文件名
     * @param inputStream 文件输入流对象
     * @return 上传之后访问阿里云Oss的Url地址
     */
    public String upload(String fileName, InputStream inputStream){
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        String obejectName = uuid+suffix;
            ossBean.putObject(config.getBucketName(),fileName,inputStream);
            String url = config.getUrl()+obejectName;
            return url;

    }
}
